# -*- coding:utf-8 -*-
from docx.shared import Inches
from docx import Document
import docx
import json
import sys
import datetime
import random
reload(sys)
sys.setdefaultencoding('utf-8')

class generateDocByJson:
	"""
	 according to the json data,
	 generating the file of docx
	"""
	def __init__(self):
		self.document = Document()
		self.document.styles['Normal'].name = '微软雅黑'.decode("utf-8")

	def __loadJsonData(self,filepath):
		jsonfile = file(filepath)
		jsonDict = json.load(jsonfile)
		jsonfile.close()
		return jsonDict

	def __generateByJsonDict(self,jsonDict,filepath):
		"""
		h1 一、当前研发人力重点工作安排
		1 table groupProject
		2 table groupManager
		"""
		self.document.add_heading('一、当前研发人力重点工作安排'.decode('utf-8'),level=1)
		self.document.add_paragraph('1. 项目人员状态表格（总人力:%.2f)'.decode('utf-8')%jsonDict['total'])

		projectTabCols = 6
		projectTab = self.document.add_table(rows=1,cols=projectTabCols)
		projectHeader_cells = projectTab.rows[0].cells
		projectHeader_cells[0].text = '项目名称'.decode('utf-8')
		projectHeader_cells[1].text = '状态'.decode('utf-8')
		projectHeader_cells[2].text = '项目经理/开发组长'.decode('utf-8')
		projectHeader_cells[3].text = '起止时间'.decode('utf-8')
		projectHeader_cells[4].text = '研发人员'.decode('utf-8')
		projectHeader_cells[5].text = '人月'.decode('utf-8')

		for rowDict in jsonDict['groupProject']:
			index = 0
			row_cells = projectTab.add_row().cells
			row_cells[0].text = rowDict['name']
			row_cells[1].text = rowDict['status']
			row_cells[2].text = rowDict['leader']
			row_cells[3].text = rowDict['date']
			row_cells[4].text = "\n".join(rowDict['detail'])
			row_cells[5].text = str(rowDict['total'])

		self.document.add_paragraph('2. 研发人员组织结构'.decode('utf-8'))

		managerTabCols = 3
		managerTab = self.document.add_table(rows=1,cols=managerTabCols)
		managerHeader_cells = managerTab.rows[0].cells
		managerHeader_cells[0].text = '组长'.decode('utf-8')
		managerHeader_cells[1].text = '组员'.decode('utf-8')
		managerHeader_cells[2].text = '承担项目中人力分配'.decode('utf-8')


		for rowDict in jsonDict['groupManager']:
			row_cells = managerTab.add_row().cells
			row_cells[0].text = rowDict['leader']
			row_cells[1].text = "\n".join(rowDict['team'])
			for key in rowDict['detail']:
				row_cells[2].text = str(key) +":\n" +"\n".join(rowDict['detail'][key])

		self.document.add_heading('二、研发存在的问题或风险，以及采取的措施。'.decode('utf-8'),level=1)

		for key in jsonDict['extraRisk']:
			p = self.document.add_paragraph("")
			p.add_run(key).bold = True
			for content in jsonDict['extraRisk'][key]:
				self.document.add_paragraph(content)

		self.document.add_heading('三、人员成长情况，后续培养计划'.decode('utf-8'),level=1)

		for key in jsonDict['extraGrown']:
			p = self.document.add_paragraph("")
			p.add_run(key).bold = True
			for content in jsonDict['extraGrown'][key]:
				self.document.add_paragraph(content)

		self.document.add_heading('四、人员变动、后续安排与其他信息'.decode('utf-8'),level=1)

		for key in jsonDict['extraOtherInfo']:
			p = self.document.add_paragraph("")
			p.add_run(key).bold = True
			for content in jsonDict['extraOtherInfo'][key]:
				self.document.add_paragraph(content)
		docxname = "app/temp/"+filepath[7:20]+".docx"
		self.document.save(docxname)
		print(docxname)


	def generateByJsonFile(self):
		filepath = raw_input()
		jsonDict = self.__loadJsonData(filepath)
		try:
			self.__generateByJsonDict(jsonDict,filepath)
		except Exception,e:
			print e

if __name__ == '__main__':
	doc =  generateDocByJson()
	doc.generateByJsonFile()